import 'package:flutter/material.dart';
import 'package:p_location/common/res/common_res.dart';
import 'package:p_location/common/widget/norm_item.dart';
import 'package:p_location/home/constant/home_http_url.dart';
import 'package:p_location/home/obj/vehicle_guige_info.dart';
import 'package:p_location/home/obj/vehicle_guige_list.dart';
import 'package:p_location/home/res/car_owner_detail_obj.dart';
import 'package:p_location/home/res/res_car_owner_detail.dart';
import 'package:p_location/net/dio_http_client.dart';
import 'package:p_location/net/net_listener.dart';
import 'package:dio/dio.dart';
import 'dart:convert' as convert;

class PageInnerCarDetail extends StatefulWidget {
  final int id;
  final String name;

  PageInnerCarDetail(this.id, this.name);

  @override
  _PageGuestDetailState createState() => _PageGuestDetailState();
}

class _PageGuestDetailState extends State<PageInnerCarDetail>
    implements NetListener {
  CarOwnerDetailObj _list;
  List<VehicleGuigeInfo> guige;
  bool _isLoading = true;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    _requestDetail();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              onPressed: () {
                Navigator.pop(context);
                // 不写onPressed 默认，这个图片不能点击 且会有不可点击的样式（和 写了这个有不同的样式）
                // 如果有 onPressed 但是值是null 也会是不可点击的样式
              }),
          title: Text(widget?.name ?? ""),
          centerTitle: true,
          elevation: 0,
          brightness: Brightness.dark,
        ),
        body: _isLoading
            ? Center(child: CircularProgressIndicator(),)
            : Container(
                child: ListView(
                  children: [
                    NormItem("车牌号码", _list.carNumber),
                    NormItem("车辆类型", _list.vehicleType),
                    NormItem("品牌", _getFieldValue("vehicleBrand")),
                    NormItem("型号", _getFieldValue("vehicleModel")),
                    NormItem("运单号", _getFieldValue("waybillNumber")),
                    NormItem("载重量(吨)", _getFieldValue("deadCapacity")),
                    NormItem("归属部门", _getFieldValue("attributionDepartment")),
                    NormItem("身份", /*_list.remark*/'内部车辆'),
                    NormItem("登记日期", _list.getIntoTime),
                    NormItem("备注", _list.remark),
                  ],
                ),
              ));
  }

  _requestDetail() async {
    Map<String, dynamic> params = {};
    params["id"] = widget.id;
    await DioHttpClient.requestGet(
        context, HomeHttpUrl.OWNER_CAT_DETAIL_URL, params, this);
  }
  
  _getFieldValue(fieldName){
    if(guige != null && guige.length > 0){
      print(guige[0]);
      VehicleGuigeInfo info = guige.firstWhere((element) => element.fieldName == fieldName, orElse: () => null);
      if(info != null){
        return info.fieldValue;
      }
    }
    return "";
  }

  @override
  void onRequestSuccess(Response response) {
    if (response != null && response.data != null) {
      ResCarOwnerDetail res = ResCarOwnerDetail.fromJson(response.data);
      if (res != null && res.data != null) {
        _isLoading = false;
        if (res != null && res.data != null) {
          _list = res.data;
          if (_list.vehicleInformation != null && _list.vehicleInformation.length > 0) {
            Map<String, dynamic> information = {
              "guige": convert.jsonDecode(_list.vehicleInformation)
            };
            VehicleGuigeList informationList = VehicleGuigeList.fromJson(information);
            if(informationList != null){
              guige  = informationList.guige;
            }
            print("informationList的size");
            print(informationList?.guige?.length??0);
          }
        }
        setState(() {});
      }
    }
  }

  @override
  void onRequestError(CommonRes errorRes) {}
}
